<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>this</title>
    <script>
        
        function Person(color) {
            console.log(this);
            this.color = color;
            this.getColor = function () {
                console.log(this);
                return this.color;
            };
            this.setColor = function (color) {
                console.log(this);
                this.color = color;
            };
        }
        
        Person('red'); // this -> window
        
        var p = new Person('yellow'); //当前new的对象来调用，this->p
        p.getColor(); //p
        p.setColor('gray'); //this->p
        
        var obj = {};
        p.setColor.call(obj, 'red'); // this->obj
        
        var test = p.getColor;
        test(); // this -> window
        
        function fun1() {
            function fun2() {
                console.log(this);
            }
            fun2();
        }
        
        fun1(); //this->window

        /**
         * 任何函数本质上都是通过某个对象调用的，如果没有指定，就是window去调用
         * 所有函数内部都有变量this，是调用函数的当前对象
         */
    </script>
</head>
<body>
    
</body>
</html>
